﻿Imports System.IO
Imports System.Threading

Public Class frmAdvOpen
    Dim PlayFormat As String
    Dim PlayObject As String
    Dim DrivesPopu As Boolean = False
    Dim allDrives() As DriveInfo = DriveInfo.GetDrives()
    Dim d As DriveInfo
    Dim NoOfDrives As Integer = 0
    Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, ByVal lpSecurityAttributes As IntPtr, ByVal dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As IntPtr) As IntPtr
    Private Declare Function DeviceIoControl Lib "kernel32" (ByVal hDevice As IntPtr, ByVal dwIoControlCode As Integer, ByVal lpInBuffer As IntPtr, ByVal nInBufferSize As Integer, ByVal lpOutBuffer As IntPtr, ByVal nOutBufferSize As Integer, ByRef lpBytesReturned As Integer, ByVal lpOverlapped As IntPtr) As Integer
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As IntPtr) As Integer
    Private Const INVALID_HANDLE_VALUE As Integer = -1
    Private Const OPEN_EXISTING As Integer = 3
    Private Const GENERIC_READ As Integer = &H80000000
    Private Const GENERIC_WRITE As Integer = &H40000000
    Private Const IOCTL_STORAGE_EJECT_MEDIA As Integer = 2967560

    Private Sub frmAdvOpen_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'OpnFile("dvd://D:\")
        'OpnFile("screen://")
        For Each d In allDrives
            If d.DriveType = DriveType.CDRom And DrivesPopu = False Then
                cbDrives.Items.Add(d.Name)
                cbDrives.SelectedIndex = 0
                NoOfDrives = +1
            End If
        Next
        If NoOfDrives = 0 Then
            cbDrives.Items.Add("No Drives Detected")
            cbDrives.SelectedIndex = 0
        End If
        DrivesPopu = True
    End Sub

    Public Sub LoadTab(ByVal StartupTab As Integer)
        tabcAdvOpen.SelectTab(StartupTab)
    End Sub

    Private Sub rbDVD_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbDVD.CheckedChanged
        If rbDVD.Checked = True Then
            PlayFormat = "dvd://"
            PlayObject = cbDrives.SelectedItem.ToString
        End If
    End Sub

    Private Sub rbAudioCD_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbAudioCD.CheckedChanged
        If rbAudioCD.Checked = True Then
            PlayFormat = "cdda://"
            PlayObject = cbDrives.SelectedItem.ToString
        End If
    End Sub

    Private Sub rbVCD_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbVCD.CheckedChanged
        If rbVCD.Checked = True Then
            PlayFormat = "vcd://"
            PlayObject = cbDrives.SelectedItem.ToString
        End If
    End Sub

    Private Sub cbDrives_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbDrives.SelectedIndexChanged
        PlayObject = cbDrives.SelectedItem.ToString
    End Sub

    Private Sub btnEject_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEject.Click
        If NoOfDrives > 0 Then
            EjectCDRom(cbDrives.SelectedItem.ToString.Remove(1, 2))
        End If
    End Sub

    Private Sub EjectThread()
        EjectCDRom(cbDrives.SelectedItem.ToString.Remove(1, 2))
    End Sub

    Private Sub EjectCDRom(ByVal driveletter As String)
        Dim path As String = "\\.\" + driveletter
        If Not path.EndsWith(":") Then path = path + ":"
        Dim hDrive As IntPtr = CreateFile(path, GENERIC_READ Or GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero)
        If CInt(hDrive) = INVALID_HANDLE_VALUE Then Throw New IO.IOException("Could not open drive " + driveletter)
        Dim dummy As Integer
        DeviceIoControl(hDrive, IOCTL_STORAGE_EJECT_MEDIA, IntPtr.Zero, 0, IntPtr.Zero, 0, dummy, IntPtr.Zero)
        CloseHandle(hDrive)
    End Sub

    Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
        Dim DidWork As Integer = DiscBrowse.ShowDialog()
        If DidWork = DialogResult.Cancel Then
        Else
            cbDrives.Text = DiscBrowse.SelectedPath
            PlayObject = DiscBrowse.SelectedPath
        End If
    End Sub

    Private Sub btnPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlay.Click
        If Not PlayFormat = "" Then
            frmMain.OpnFile(PlayFormat & PlayObject)
            Me.Close()
        End If
    End Sub
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub gbNetworkProtocol_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gbNetworkProtocol.Enter
        PlayFormat = ""
        PlayObject = ""
    End Sub

    Private Sub cbProtocol_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbProtocol.SelectedIndexChanged
        PlayFormat = cbProtocol.SelectedItem.ToString.ToLower & "://"
        If cbProtocol.SelectedIndex = 6 Then
            nudPort.Enabled = True
            cbTimeshifting.Enabled = True
            PlayObject = txtAddress.Text & "@:" & nudPort.Value & " :access-filter=timeshift"
        ElseIf (cbProtocol.SelectedIndex = 5 Or cbProtocol.SelectedIndex = 6) Then
            nudPort.Enabled = True
            cbTimeshifting.Enabled = False
            PlayObject = txtAddress.Text & "@:" & nudPort.Value
        Else
            nudPort.Enabled = False
            cbTimeshifting.Enabled = False
            PlayObject = txtAddress.Text
        End If
    End Sub

    Private Sub txtAddress_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAddress.TextChanged
        If Not cbProtocol.SelectedIndex = -1 Then
            PlayFormat = cbProtocol.SelectedItem.ToString.ToLower & "://"
        End If
        If Not cbProtocol.SelectedIndex = -1 And cbProtocol.SelectedIndex = 6 Then
            nudPort.Enabled = True
            cbTimeshifting.Enabled = True
            PlayObject = txtAddress.Text & "@:" & nudPort.Value & " :access-filter=timeshift"
        ElseIf Not cbProtocol.SelectedIndex = -1 And (cbProtocol.SelectedIndex = 5 Or cbProtocol.SelectedIndex = 6) Then
            nudPort.Enabled = True
            cbTimeshifting.Enabled = False
            PlayObject = txtAddress.Text & "@:" & nudPort.Value
        Else
            nudPort.Enabled = False
            cbTimeshifting.Enabled = False
            PlayObject = txtAddress.Text
        End If
    End Sub

    Private Sub cbTimeshifting_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbTimeshifting.CheckedChanged
        If Not cbProtocol.SelectedIndex = -1 And (cbProtocol.SelectedIndex = 6) And cbTimeshifting.Checked = True Then
            nudPort.Enabled = True
            PlayObject = txtAddress.Text & "@:" & nudPort.Value & " :access-filter=timeshift"
        Else
            PlayObject = txtAddress.Text & "@:" & nudPort.Value
        End If
    End Sub
End Class